package com.example.customviewtest.clipImage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.customviewtest.R;

/**
 * @auther XuDan on 2020/4/22.
 */
public class ClipFiveStarView extends View {
    private Context context;
    private Paint mPaint;
    private String imgPath;
    private Paint mBmpPaint;
    private int mWidth;
    private int mHeight;
    private Bitmap mReSize_bitmap;

    private static int circle = 800;
    private static int angle = 360 / 5;
    private Point mCenter_point;
    private static int R = 400;
    private static int r = 200;
    private Path mCanvas_path;


    public ClipFiveStarView(Context context) {
        this(context, null);
    }

    public ClipFiveStarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipFiveStarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mBmpPaint = new Paint();
        mCanvas_path = new Path();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        int mHalf_width = mWidth / 2;
        int mHalf_height = mHeight / 2;
        mCenter_point = new Point(mHalf_width, mHalf_height);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        mReSize_bitmap = reSize(bitmap);
        canvas.drawBitmap(mReSize_bitmap, 0, 0, mBmpPaint);
        drawFiveStar(canvas);
    }

    private Bitmap reSize(Bitmap bitmap) {
        int toWidth = mWidth, toHeight = mHeight;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int resizeWidth, resizeHeight, x, y;
        float rate = (float) (1.0 * toWidth / toHeight);
        if (1.0 * width / height == rate)
            return bitmap;
        if (1.0 * width / height > rate) {
            resizeHeight = height;
            resizeWidth = (int) (height * rate);
            x = (width - resizeWidth) / 2;
            y = 0;
        } else {
            resizeWidth = width;
            resizeHeight = (int) (width / rate);
            x = 0;
            y = (height - resizeHeight) / 2;
        }
        Bitmap b = Bitmap.createBitmap(bitmap, x, y, resizeWidth, resizeHeight);
        if (resizeHeight == toHeight) {
            return b;
        } else {
            Matrix matrix = new Matrix();
            matrix.preScale((float) 1.0 * toWidth / resizeWidth, (float) 1.0 * toHeight / resizeHeight);
            return Bitmap.createBitmap(b, 0, 0, resizeWidth, resizeHeight, matrix, false);
        }
    }


    private void drawFiveStar(Canvas canvas) {
        canvas.save();
        Log.i("xd---------", mCenter_point.x + "--------" + mCenter_point.y);
        canvas.translate(mCenter_point.x, mCenter_point.y);
        for (int i = 0; i < 5; i++) {
            double outter_x = Math.cos((18 + 72 * i) / 180 * Math.PI) * R + mCenter_point.x;
            double outter_y = -Math.sin((18 + 72 * i) / 180 * Math.PI) * R + mCenter_point.y;
            double inner_x = Math.cos((54 + 72 * i) / 180 * Math.PI) * r + mCenter_point.x;
            double inner_y = -Math.sin((54 + 72 * i) / 180 * Math.PI) * r + mCenter_point.y;
            Log.i("xd---------", "outer====" + outter_x + "==" + outter_y);
            Log.i("xd---------", "inner====" + inner_x + "==" + inner_y);
            mCanvas_path.lineTo((float) outter_x, (float) outter_y);
            mCanvas_path.lineTo((float) inner_x, (float) inner_y);
//            if (i == 0) {
//                mCanvas_path.moveTo((float) outter_x, (float) outter_y);
//                mCanvas_path.lineTo((float) inner_x, (float) inner_y);
//            } else {
//                mCanvas_path.lineTo((float) outter_x, (float) outter_y);
//                mCanvas_path.lineTo((float) inner_x, (float) inner_y);
//            }
        }
        mCanvas_path.close();
        canvas.drawPath(mCanvas_path, mPaint);


    }

    int dx = 0;
    int dy = 0;
    float last_y = 0;
    float last_x = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                last_x = event.getRawX();
                last_y = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                dx = (int) (event.getRawX() - last_x);
                dy = (int) (event.getRawY() - last_y);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    public void setImagePath(String path) {
        this.imgPath = path;
        invalidate();
    }

    public Bitmap getClipBitmap() {
//        Bitmap bitmap = Bitmap.createBitmap(mReSize_bitmap, mLeft, mTop);
        return null;

    }

    public void recycle() {
        if (mReSize_bitmap != null && !mReSize_bitmap.isRecycled()) {
            mReSize_bitmap.recycle();
        }
    }
}
